<!-- 类别浏览 -->
<template>
	<div class="t-wp" v-loading="state.loading">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form class="text-right" :model="state.queryParams" ref="queryForm" :inline="true">
				<!-- <el-form-item label="类别名称">
					<el-input v-model="state.queryParams.aaa" placeholder="请输入"/>
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery"> 查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> 重置 </el-button>
					</el-button-group>
				</el-form-item> -->
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="showAddCategory()"> 新增 </el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="full-table" shadow="hover" style="margin-top: 8px">
			<el-table :data="state.list" style="width: 100%" border @expand-change="expandChange">
				<el-table-column type="expand">
					<template #default="scope">
						<ProcessTable :ref="setRefAction" :id="scope.row.id" @edit="showAddProcess"></ProcessTable>
					</template>
				</el-table-column>
				<el-table-column type="index" label="序号" width="55" align="center" />
				<el-table-column prop="projectName" label="类别名称" show-overflow-tooltip />
				<el-table-column label="操作" width="350" align="center" fixed="right" show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="el-icon-edit" size="small" text type="primary" @click="showAddCategory(scope.row)">编辑类别</el-button>
						<el-button icon="el-icon-delete" size="small" text type="primary" @click="deleteRow(scope.row)">删除类别</el-button>
						<el-button icon="el-icon-plus" size="small" text type="primary" @click="showAddProcess(scope.row.id)">添加阶段</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>

		<CategoryModal ref="CategoryModalRef" @reload="handleQuery"></CategoryModal>
		<ProcessModal ref="ProcessModalRef" @reload="reloadProcessTable"></ProcessModal>
	</div>
</template>

<script setup name="SupplierManage">
import { onMounted, reactive, ref } from 'vue';
import {useRouter, useRoute} from "vue-router";
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus';
import CategoryModal from "./modules/CategoryModal.vue";
import ProcessTable from "./modules/ProcessTable.vue";
import ProcessModal from "./modules/ProcessModal.vue";
import {$get, $post, $postparams} from '/@/utils/request';

const router = useRouter();
const route = useRoute();
const CategoryModalRef = ref();
const ProcessModalRef = ref();
const ProcessTableRef = ref();

const ProcessTableRefList = ref([]);
const setRefAction = function (el) {
	if(el) {
		if(!ProcessTableRefList.value.find(item => item.id == el.id)) {
			ProcessTableRefList.value.push(el);
		}
	}
}

const PUBLIC_PATH = import.meta.env.VITE_PUBLIC_PATH;
const state = reactive({
	loading: false,
	queryParams: {
		aaa: '',
		bbb: '',
	},
	list: [
		// {aa1: '2-5万（含2万，不含5万）', aa2: '1', aa3: '0'},
		// {aa1: '5-20万（含5万，不含20万）', aa2: '2', aa3: '0'},
		// {aa1: '20-50万（含20万，不含50万）', aa2: '3', aa3: '0'},
	],
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	let params = {
		input: 0
	}
	var res = await $get('/projectTemplateModule/getprojecttemplate', params);
	state.list = (res.result || []);
	state.loading = false;
};

// 关闭行
const expandChange = (row, expandedRows) => {
	let expanded = expandedRows.find(item => item.id == row.id) ? true : false;
	if(!expanded) {
		let index = ProcessTableRefList.value.findIndex(item => item.id == row.id);
		ProcessTableRefList.value.splice(index, 1);
	}
}

// 重置操作
const resetQuery = () => {
	state.queryParams.aaa = '';
	state.queryParams.bbb = '';
	handleQuery();
};

// 显示添加类别
const showAddCategory = (row) => {
	CategoryModalRef.value.open(row);
}

// 删除类别
const deleteRow = (row) => {
	ElMessageBox.confirm('此操作将删除该类别，是否继续？', '提示', {
		type: 'warning'
	}).then(async () => {
		let params = {
			input: row.id
		}
		var res = await $postparams('/projectTemplateModule/removeprojecttemplate', params);
		ElNotification({
	    title: '提示',
	    message: '删除成功',
	    type: 'success',
	  })
	  // 更新列表
	  handleQuery();
	})
}

// 显示/编辑添加过程
const showAddProcess = (id, detail) => {
	ProcessModalRef.value.open(id, detail);
}

// 更新阶段信息
const reloadProcessTable = (id) => {
	let oRef = ProcessTableRefList.value.find(item => item.id == id);
	if(oRef) {
		oRef.reload();
	}
}

</script>

<style lang="scss" scoped>
.t-wp {
	
}
</style>
